﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Moliser3
{

    public static class Moliser3Tool
    {
        static Vector3 vecA;
        static Vector3 vecB;
        static int crossNumber = 0;
        public static bool IsLinesIntersectInPlaneXZ(Vector3[] lineA, Vector3[] lineB)
        {
            foreach (var item in lineB)
            {
                if (lineA[0] == item || lineA[1] == item)
                {
                    return true;
                }
            }
            vecA = lineA[1] - lineA[0];
            vecB = lineB[1] - lineB[0];
            //叉乘判断
            if (
                (Vector3.Cross(vecA, lineB[0] - lineA[0]).y >= 0 && Vector3.Cross(vecA, lineB[1] - lineA[0]).y <= 0)
                ||
                (Vector3.Cross(vecA, lineB[0] - lineA[0]).y <= 0 && Vector3.Cross(vecA, lineB[1] - lineA[0]).y >= 0)
                )
                crossNumber++;
            if (
                (Vector3.Cross(vecB, lineA[0] - lineB[0]).y >= 0 && Vector3.Cross(vecB, lineA[1] - lineB[0]).y <= 0)
                ||
                (Vector3.Cross(vecB, lineA[0] - lineB[0]).y <= 0 && Vector3.Cross(vecB, lineA[1] - lineB[0]).y >= 0)
                )
                crossNumber++;

            if (crossNumber != 0 && crossNumber % 2 == 0)
            {
                crossNumber = 0;
                return true;
            }
            else
            {
                crossNumber = 0;
                return false;
            }

        }
    }
}
